using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using EasyCommerce.BLL;
using EasyCommerce.DAL;
using System.Data.SqlTypes;
using CodeWork.Library;
using CodeWork.Library.Web;

public partial class admin_SettingsEdit : BasePage
{
    private SecurityLevel _securityLevel = SecurityLevel.One;

    protected void Page_Load(object sender, EventArgs e)
    {
        Title = _siteName;

        if (!IsPostBack)
        {
            if (Request["id"] != null)
                LoadData();
        }
    }

    private void LoadData()
    {
        int id = 0;
        Settings settings;

        if (Int32.TryParse(Request["id"], out id))
            settings = Settings.Load(id);
        else
            settings = null;

        if (settings != null)
        {
            if (!string.IsNullOrEmpty(settings.SiteName))
				tbxSiteName.Text = settings.SiteName;
			if (!string.IsNullOrEmpty(settings.CurrentDomain))
				tbxCurrentDomain.Text = settings.CurrentDomain;
			if (!string.IsNullOrEmpty(settings.AdminEmail))
				tbxAdminEmail.Text = settings.AdminEmail;
			if (!string.IsNullOrEmpty(settings.SmtpServer))
				tbxSmtpServer.Text = settings.SmtpServer;
			if (!string.IsNullOrEmpty(settings.SmtpUsername))
				tbxSmtpUsername.Text = settings.SmtpUsername;
			if (!string.IsNullOrEmpty(settings.SmtpPassword))
				tbxSmtpPassword.Text = settings.SmtpPassword;
			tbxSmtpPort.Text = settings.SmtpPort.ToString();
			cbxSmtpUseSsl.Checked = settings.SmtpUseSsl;
			if (!string.IsNullOrEmpty(settings.EncryptionKey))
				tbxEncryptionKey.Text = settings.EncryptionKey;
			if (!string.IsNullOrEmpty(settings.SupportMail))
				tbxSupportMail.Text = settings.SupportMail;
			if (!string.IsNullOrEmpty(settings.DefaultTheme))
				tbxDefaultTheme.Text = settings.DefaultTheme;
			tbxDefaultTimeZone.Text = settings.DefaultTimeZone.ToString();
			tbxDefaultUserRoleId.Text = settings.DefaultUserRoleId.ToString();

            ViewState["id"] = id;
        }
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        string siteName = Security.BindStringInput(tbxSiteName.Text, _securityLevel);
		string currentDomain = Security.BindStringInput(tbxCurrentDomain.Text, _securityLevel);
		string adminEmail = Security.BindStringInput(tbxAdminEmail.Text, _securityLevel);
		string smtpServer = Security.BindStringInput(tbxSmtpServer.Text, _securityLevel);
		string smtpUsername = Security.BindStringInput(tbxSmtpUsername.Text, _securityLevel);
		string smtpPassword = Security.BindStringInput(tbxSmtpPassword.Text, _securityLevel);
		int smtpPort = Security.BindIntegerInput(tbxSmtpPort.Text, _securityLevel);
		bool smtpUseSsl = cbxSmtpUseSsl.Checked;
		string encryptionKey = Security.BindStringInput(tbxEncryptionKey.Text, _securityLevel);
		string supportMail = Security.BindStringInput(tbxSupportMail.Text, _securityLevel);
		string defaultTheme = Security.BindStringInput(tbxDefaultTheme.Text, _securityLevel);
		int defaultTimeZone = Security.BindIntegerInput(tbxDefaultTimeZone.Text, _securityLevel);
		int defaultUserRoleId = Security.BindIntegerInput(tbxDefaultUserRoleId.Text, _securityLevel);

        if (ViewState["id"] != null)
        {
            int id = 0;
            if (int.TryParse(ViewState["id"].ToString(), out id))
            {
                if (Settings.Save(
				id, 
				siteName,
				currentDomain,
				adminEmail,
				smtpServer,
				smtpUsername,
				smtpPassword,
				smtpPort,
				smtpUseSsl,
				encryptionKey,
				supportMail,
				defaultTheme,
				defaultTimeZone,
				defaultUserRoleId
                ))
                    Response.Redirect("settingslist.aspx");
                else
                    ResponseMessage = "Save failed";
            }
            else
                ResponseMessage = "Save failed";
        }
        else
        {
            Settings settings = Settings.Create(
				siteName,
				currentDomain,
				adminEmail,
				smtpServer,
				smtpUsername,
				smtpPassword,
				smtpPort,
				smtpUseSsl,
				encryptionKey,
				supportMail,
				defaultTheme,
				defaultTimeZone,
				defaultUserRoleId
                );

            if (settings != null)
                Response.Redirect("settingslist.aspx");
            else
                ResponseMessage = "Failed to create user";
        }
    }

    protected void Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("settingslist.aspx");
    }
}
